package com.cskaoyan.mall.pay.service.impl;

import com.cskaoyan.mall.order.dto.PayOrderSuccessRequest;
import com.cskaoyan.mall.pay.api.OrderPayApi;
import com.cskaoyan.mall.pay.constants.PayResultEnum;
import com.cskaoyan.mall.pay.constants.PayReturnCodeEnum;
import com.cskaoyan.mall.pay.dal.entitys.Payment;
import com.cskaoyan.mall.pay.dal.persistence.PaymentMapper;
import com.cskaoyan.mall.pay.dto.PaymentRequest;
import com.cskaoyan.mall.pay.dto.alipay.AlipayQueryRetResponse;
import com.cskaoyan.mall.pay.dto.alipay.AlipaymentResponse;
import com.cskaoyan.mall.pay.service.PayCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @auther cskaoyan
 * @date 2022/6/22:11:33
 */
@Service
public class PayCoreServiceImpl implements PayCoreService {

    @Autowired
    PayHelper payHelper;
    @Autowired
    PaymentMapper paymentMapper;
    @Autowired
    OrderPayApi orderPayApi;

    @Override
    public AlipaymentResponse aliPay(PaymentRequest request) {
        AlipaymentResponse alipaymentResponse = new AlipaymentResponse();

        //  1.  访问支付宝，获取支付二维码
        String codeFileName = payHelper.test_trade_precreate(request);

        // 2. 如果获取支付二维码失败
        if (codeFileName == null) {
            alipaymentResponse.setCode(PayReturnCodeEnum.GET_CODE_FALIED.getCode());
            alipaymentResponse.setMsg(PayReturnCodeEnum.GET_CODE_FALIED.getMsg());
            return alipaymentResponse;
        }

        // 3. 向tb_payment插入一条记录，该记录中，  T支付状态为RADE_PROCESSING
        Payment payment = new Payment();
        payment.setStatus(PayResultEnum.TRADE_PROCESSING.getCode());
        payment.setCreateTime(new Date());
        payment.setUpdateTime(new Date());
        payment.setPayerUid(request.getUserId());
        payment.setPayWay(request.getPayChannel());
        payment.setOrderAmount(request.getOrderFee());
        payment.setPayerAmount(request.getTotalFee());
        payment.setOrderId(request.getTradeNo());
        paymentMapper.insert(payment);
        // 返回成功的响应
        alipaymentResponse.setQrCode(codeFileName);
        alipaymentResponse.setCode(PayReturnCodeEnum.SUCCESS.getCode());
        alipaymentResponse.setMsg(PayReturnCodeEnum.SUCCESS.getMsg());
        return alipaymentResponse;
    }

    @Override
    public AlipayQueryRetResponse queryAlipayRet(PaymentRequest request) {
        AlipayQueryRetResponse alipayQueryRetResponse = new AlipayQueryRetResponse();
        // 查询
        boolean success = payHelper.test_trade_query(request.getTradeNo());
        Payment payment = new Payment();
        payment.setCompleteTime(new Date());
        payment.setPayerAmount(request.getTotalFee());
        payment.setOrderAmount(request.getOrderFee());
        payment.setPayWay(request.getPayChannel());
        payment.setPayerUid(request.getUserId());
        if (success) {
            // 1. 修改tb_payment支付状态 TRADE_SUCCESS， 支付成功时间，支付完成时间（支付服务）
            //  订单服务(服务调用)
            // 2. 修改订单状态为支付成功
            // 3. 修改库存，查询订单中包含的所有订单条目，根据订单条目扣减每一个商品的锁定库存
            payment.setStatus(PayResultEnum.TRADE_SUCCESS.getCode());
            payment.setOrderId(request.getTradeNo());
            payment.setPaySuccessTime(new Date());
            PayOrderSuccessRequest payOrderSuccessRequest = new PayOrderSuccessRequest();
            payOrderSuccessRequest.setOrderId(request.getTradeNo());
            orderPayApi.payOrderSuccess(payOrderSuccessRequest);
            alipayQueryRetResponse.setCode(PayReturnCodeEnum.SUCCESS.getCode());
            alipayQueryRetResponse.setMsg(PayReturnCodeEnum.SUCCESS.getMsg());
        } else {
            // 1. 修改tb_payment支付状态 FAIL, 更新一下支付完成时间
            payment.setStatus(PayResultEnum.FAIL.getCode());
            payment.setOrderId(request.getTradeNo());
            alipayQueryRetResponse.setCode(PayReturnCodeEnum.PAYMENT_PROCESSOR_FAILED.getCode());
            alipayQueryRetResponse.setMsg(PayReturnCodeEnum.PAYMENT_PROCESSOR_FAILED.getMsg());
        }
        Example example = new Example(Payment.class);
        example.createCriteria().andEqualTo("orderId", request.getTradeNo());
        paymentMapper.updateByExample(payment, example);
        return alipayQueryRetResponse;
    }
}
